package com.mybase.app.minute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.SimpleAdapter;
 import android.widget.TextView;
 import android.widget.AdapterView.OnItemClickListener;
import com.mybase.app.R;

public class MultipleChoiceActivity extends Activity {

       public static final int INT = 93;
       private ListView lvImageList;

      private String imageID= "imageID";
       private String imageName = "imageName";
       private String imageInfo = "imageInfo";

      private ArrayList<String> fileNames = new ArrayList<String>();

      private MultipleChoiceImageListAdapter mAdapter;

      /** Called when the activity is first created. */
      @Override
   public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.layout_mimute_multiple);

                 lvImageList=(ListView) this.findViewById(R.id.lvImageList);
                 lvImageList.setItemsCanFocus(false);
                 lvImageList.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                 CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.itemChkImageInfo);
                                 checkedTextView.toggle();
                                 mAdapter.setCheckItem(position, checkedTextView.isChecked());
                             }
                    });
                    try{
                        String[] from = {imageID, imageName, imageInfo};
                        int[] to = {R.id.itemImgImageInfo, R.id.itemChkImageInfo, R.id.itemTxtImageInfo};
                        mAdapter = new MultipleChoiceImageListAdapter(MultipleChoiceActivity.this, GetImageList(), R.layout.layout_mimute_multiple_item, from, to);
                        lvImageList.setAdapter(mAdapter);
                    }
                 catch(Exception ex){
                        return;
                    }
             }

                //获取图片列表
                 private ArrayList<Map<String, String>> GetImageList(){

                ArrayList<Map<String, String>> imageList = new ArrayList<Map<String,String>>();
                HashMap<String, String> imageMap;

                //读取SD卡中所有图片
                Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE};
                String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
                String[] selectionArg ={"image/jpeg"};
                Cursor mCursor = this.managedQuery(uri, projection, selection, selectionArg, MediaStore.Images.Media.DISPLAY_NAME);
                imageList.clear();
                if (mCursor != null) {
                         mCursor.moveToFirst();
                         while (mCursor.getPosition() != mCursor.getCount())
                            {
                                imageMap= new HashMap<String, String>();
                                imageMap.put(imageID, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media._ID)));
                                imageMap.put(imageName, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
                                imageMap.put(imageInfo, " " + (mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE))/1024)+"KB");
                                imageList.add(imageMap);
                                fileNames.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                                mCursor.moveToNext();
                            }
                         mCursor.close();}
                return imageList;
            }

             //可多选图片列表适配器
             class MultipleChoiceImageListAdapter extends SimpleAdapter {

                 private Map<Integer, Boolean> map;
              private List<Integer> state;
              private List<? extends Map<String, ?>> mList;

                   LayoutInflater mInflater;
                         public MultipleChoiceImageListAdapter(Context context, List<Map<String, String>> data, int resource, String[] from, int[] to) {
                 super(context, data, resource, from, to);
                 map = new HashMap<Integer, Boolean>();
                 mInflater = LayoutInflater.from(context);
                 mList = data;
                 for(int i = 0; i < data.size(); i++) {                 map.put(i, false);
                  }
                 state = new ArrayList<Integer>();
             }

               @Override
       public int getCount() {
               return mList.size();
           }

               @Override
       public Object getItem(int position) {
               return position;
           }

               @Override
       public long getItemId(int position) {
               return position;
           }

               //设置条目选中状态
               public void setCheckItem(int position, Boolean isChecked){
               map.put(position, isChecked);
               if (state.contains(position))
                       state.remove((Object)position);
               if (isChecked){
                       state.add(position);
                   }
           }

               //获取列表中已选中条目
               public long[] getCheckItemIds(){
               int count = state.size();
               long[] ids = new long[count];
               for (int i = 0; i < count; i++) {
                       ids[i]= (long)state.get(i);
                   }
               return ids;
           }

               @Override
       public View getView(int position, View convertView, ViewGroup parent) {
               if(convertView == null) {
                       convertView = mInflater.inflate(R.layout.layout_mimute_multiple_item, null);
                   }

           CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.itemChkImageInfo);
           checkedTextView.setChecked(map.get(position));
           checkedTextView.setText((String)mList.get(position).get(imageName));

           TextView textView = (TextView) convertView.findViewById(R.id.itemTxtImageInfo);
           textView.setText((String)mList.get(position).get(imageInfo));

           //显示图片缩略图
           ImageView image = (ImageView) convertView.findViewById(R.id.itemImgImageInfo);
           Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong((String)mList.get(position).get(imageID)), Images.Thumbnails.MICRO_KIND, null);
           image.setImageBitmap(bm);

                      return convertView;
                  }
           }
     }
